/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ProgressDialog.java
 *
 * Created on 2012-05-16, 12:50:22
 */
package common.gui.progress;

import java.awt.Rectangle;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JProgressBar;

/**
 *
 * @author Nobody
 */
public class ProgressDialog extends JDialog implements IProgressItem {

    public ProgressDialog(JFrame frame) {
        super(frame);
        setLocationRelativeTo(frame);
        initComponents();
        setPositionInCentreOfWindow(frame);
        setVisible(true);

    }
    public static final String START_MESSAGE = "Starting...";

    public static void PerformLongAction(final ProgressDialog unclosableDialog, final IProgressableElement element) throws InterruptedException {
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                element.perform(unclosableDialog);
                }
        });
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        progress = new javax.swing.JProgressBar();
        PercentLabel = new javax.swing.JLabel();
        Log = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setAlwaysOnTop(true);
        setResizable(false);
        setUndecorated(true);

        PercentLabel.setText(" %");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(Log, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(progress, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 334, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(PercentLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 78, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(PercentLabel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 46, Short.MAX_VALUE)
                    .addComponent(progress, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(Log, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    public void afterReading() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void beforeReading() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public int getMaximum() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void readed(double readed) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void setMaximum(int maximum) {
        throw new UnsupportedOperationException("Not supported yet.");
    }



    public synchronized void setPercent(double percent) {
        PercentLabel.setText(String.format("%.1f ", percent) + "%");
        Rectangle progressRect = PercentLabel.getBounds();
        progressRect.x = 0;
        progressRect.y = 0;

        PercentLabel.paintImmediately(progressRect);

    }

    public JProgressBar getProgress() {
        return progress;
    }

    public synchronized void setLogMessage(String message) {
        Log.setText(message);
        Log.updateUI();
        Rectangle progressRect = Log.getBounds();
        progressRect.x = 0;
        progressRect.y = 0;
        Log.paintImmediately(progressRect);
    }

    public void setPositionInCentreOfWindow(JFrame window) {
        Rectangle mainWindowRectangle = window.getBounds();
        Rectangle thisWindowRectangle = getBounds();
        thisWindowRectangle.x = mainWindowRectangle.x + mainWindowRectangle.width / 2;
        thisWindowRectangle.y = mainWindowRectangle.y + mainWindowRectangle.height / 2;
        setBounds(thisWindowRectangle);

    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel Log;
    private javax.swing.JLabel PercentLabel;
    private javax.swing.JProgressBar progress;
    // End of variables declaration//GEN-END:variables
}
